using UnityEngine;

/// <summary>
/// 游戏事件管理器示例
/// </summary>
public class GameEventManager : MonoBehaviour
{
    // 定义游戏事件常量
    public static class GameEvents
    {
        public const string GAME_STARTED = "GameStarted";
        public const string GAME_PAUSED = "GamePaused";
        public const string GAME_RESUMED = "GameResumed";
        public const string GAME_OVER = "GameOver";
        public const string SCORE_CHANGED = "ScoreChanged";
        public const string PLAYER_DIED = "PlayerDied";
        public const string LEVEL_COMPLETED = "LevelCompleted";
    }

    void Awake()
    {
        // 注册游戏事件监听器
        EventCenter.AddListener(GameEvents.GAME_STARTED, OnGameStarted);
        EventCenter.AddListener(GameEvents.GAME_PAUSED, OnGamePaused);
        EventCenter.AddListener(GameEvents.GAME_RESUMED, OnGameResumed);
        EventCenter.AddListener<int>(GameEvents.SCORE_CHANGED, OnScoreChanged);
        EventCenter.AddListener(GameEvents.PLAYER_DIED, OnPlayerDied);
    }

    void OnDestroy()
    {
        // 移除事件监听器
        EventCenter.RemoveListener(GameEvents.GAME_STARTED, OnGameStarted);
        EventCenter.RemoveListener(GameEvents.GAME_PAUSED, OnGamePaused);
        EventCenter.RemoveListener(GameEvents.GAME_RESUMED, OnGameResumed);
        EventCenter.RemoveListener<int>(GameEvents.SCORE_CHANGED, OnScoreChanged);
        EventCenter.RemoveListener(GameEvents.PLAYER_DIED, OnPlayerDied);
    }

    void Start()
    {
        // 触发游戏开始事件
        EventCenter.TriggerEvent(GameEvents.GAME_STARTED);
    }

    #region 事件处理函数

    private void OnGameStarted()
    {
        Debug.Log("游戏已开始");
        // 可以在这里初始化游戏对象、UI等
    }

    private void OnGamePaused()
    {
        Debug.Log("游戏已暂停");
        // 可以在这里暂停游戏逻辑、显示暂停菜单等
    }

    private void OnGameResumed()
    {
        Debug.Log("游戏已恢复");
        // 可以在这里恢复游戏逻辑、隐藏暂停菜单等
    }

    private void OnScoreChanged(int newScore)
    {
        Debug.Log("分数更新: " + newScore);
        // 可以在这里更新UI分数显示等
    }

    private void OnPlayerDied()
    {
        Debug.Log("玩家死亡");
        // 可以在这里处理玩家死亡逻辑
    }

    #endregion

    #region 公共方法（供其他脚本调用以触发事件）

    public void PauseGame()
    {
        EventCenter.TriggerEvent(GameEvents.GAME_PAUSED);
    }

    public void ResumeGame()
    {
        EventCenter.TriggerEvent(GameEvents.GAME_RESUMED);
    }

    public void UpdateScore(int score)
    {
        EventCenter.TriggerEvent<int>(GameEvents.SCORE_CHANGED, score);
    }

    public void PlayerDie()
    {
        EventCenter.TriggerEvent(GameEvents.PLAYER_DIED);
    }

    #endregion
}